/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.AnnotationClasses;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreePath;

/**
 *
 * @author leng
 */
/**tree editor for tree node of annoatated classes.*/
public class Treeview_ClassNodeEditor extends AbstractCellEditor implements TreeCellEditor {

    Treeview_NodeRenderer renderer = new Treeview_NodeRenderer();
    
    ChangeEvent changeEvent = null;
    JTree tree;

    public Treeview_ClassNodeEditor(JTree tree) {
        this.tree = tree;
    }

    /**get object before modifying.*/
    public Object getCellEditorValue() {
        //System.out.println("01 - must return 11 or 22 ");
        Object o = renderer.getClassRenderer();

        if ( o instanceof JClassCheckBox ) {
            //System.out.println("     - 11");
            JClassCheckBox checkbox = (JClassCheckBox) renderer.getClassRenderer();
            Treeview_ClassNode checkBoxNode = new Treeview_ClassNode(
                    checkbox.getText(), checkbox.isSelected(),
                    checkbox.getClassColor(),
                    checkbox.getAnnotationAmount(), checkbox.getAnnotationTypeAmount() );
            //System.out.println("current selected item is: " + checkbox.getText() + " :: " + checkbox.isSelected() );

            // now, the time point is chechbox status changed but the display component are still not get assembled.
            // so we build out own status monitor here.
            ShowClassesInTreeView scitv = new ShowClassesInTreeView();
            scitv.checkStatusChanged( checkbox.getClassname().trim() , checkbox.isSelected() );

            return checkBoxNode;

        }/*else if (o instanceof JAnnotationCheckBox ) {
            //System.out.println("     - 22");

            JAnnotationCheckBox label = (JAnnotationCheckBox)renderer.getClassRenderer();
            Treeview_AnnotationNode annotationnode = new Treeview_AnnotationNode( label.getUniqueAnnotationText(), label.getUniqueAnnotationAmount() );
            annotationnode.isNotAppeared_in_CurrentArticle = label.isNotAppeared_in_CurrentArticle;          
            annotationnode.setClassColor( label.iconcolor );            
            return annotationnode;
        }*/

        //System.out.println("error 1008260008 - miss type of cell editor type!!!");

        return o;
    }

    /**To a node pointted by mouse, justify whether it is editable or not .*/
    @Override
    public boolean isCellEditable(EventObject event) {
        boolean returnValue = false;
        if (event instanceof MouseEvent) {
        MouseEvent mouseEvent = (MouseEvent) event;
        TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
        if (path != null) {
            Object node = path.getLastPathComponent();
                if ((node != null) && ( node instanceof Treeview_ClassNode )) {
                    //DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                    //Object userObject = treeNode.getUserObject();
                    returnValue = true;//= ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
                }
            }
        }
        //System.out.println("editable = " + returnValue);
        return returnValue;
    }

    /**add listener to jclasscheckbox to answer require.*/
    public Component getTreeCellEditorComponent(JTree tree, Object value,
        boolean selected, boolean expanded, boolean leaf, int row) {

        Component editor = renderer.getTreeCellRendererComponent(tree, value,
            true, expanded, leaf, row, true);
           // System.out.println(" 00 %%%%%%%%%%%%%%% - you must see 11 or 22 index following:" + editor.toString());
        // editor always selected / focused
        ItemListener itemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                if (stopCellEditing()) {
                    fireEditingStopped();
                }
            }
        };

        if ( editor instanceof JClassCheckBox ) {
            //System.out.println(" 11 jclasscheckbox event blinded %%%%%%%%%%%%%%%");
            ((JClassCheckBox) editor).addItemListener(itemListener);
        }//else if ( editor instanceof JAnnotationCheckBox ) {
        //    ((JAnnotationCheckBox) editor).addItemListener(itemListener);
        //    //System.out.println(" 22 jannotationcheckbox enent blinded %%%%%%%%%%%%%%%");
        //}

        return editor;
    }
}